package com.itcast.mobileController;

import cn.hutool.core.lang.Assert;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.itcast.health.constant.MessageConstant;
import com.itcast.health.constant.RedisMessageConstant;
import com.itcast.health.pojo.Form;
import com.itcast.health.pojo.Result;
import com.itcast.health.utils.JedisUtil;
import com.itcast.health.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;


/**
 * @author Wang
 * @version v1.0
 * @date 2019/11/26 20:20
 * @description 发送短信
 **/
@RestController
@RequestMapping("validateCode")
public class ValiDateCode {
    @Autowired
    JedisUtil jedisUtil;
    @Autowired
    DefaultKaptcha defaultKaptcha;

    @RequestMapping("/code/{deviceId}")
    public void createCode(@PathVariable String deviceId, HttpServletResponse response) throws Exception {
        Assert.notNull(deviceId, "机器码不能为空");
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");
        //生成文字验证码
        String text = defaultKaptcha.createText();
        //生成图片验证码
        BufferedImage image = defaultKaptcha.createImage(text);
        //生成的验证码写入redis
       jedisUtil.setex(deviceId,60*5,text);
        //获取输出流
        ServletOutputStream out = response.getOutputStream();
        //将图片写回浏览器
        ImageIO.write(image, "JPEG", out);
    }



    /**
     * 预约时发送的验证码
     */
    @RequestMapping("send4Order")
    public Result send4Order(@RequestBody Form form) {

        //利用工具类生成4位数的验证码
        Integer code = ValidateCodeUtils.generateValidateCode(4);
        try {
            //用户输入的验证码
            String imgCode = form.getImgCode();
            //从redis中取出图片验证码与用户输入的比较
            String deviceId = form.getDeviceId();
            String redisImgCode = jedisUtil.get(deviceId);
           // if (imgCode!=null && imgCode.equals("")){
                if (!imgCode.equals(redisImgCode)){
                    //用户输入的图片验证码不正确
                    return Result.error("图片验证码输入错误");
                }
//            }else {
//                return Result.error("图片验证码不能为空");
//            }
            //调用短信工具类给用户发短信
            //  SmsUtil.sendSmsCode(form.getTelephone(),code.toString());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(MessageConstant.SEND_VALIDATECODE_FAIL);
        }
        System.out.println("验证码为:" + code);
        //发送成功,将验证码存入redis中
        jedisUtil.setex(form.getTelephone() + RedisMessageConstant.SENDTYPE_ORDER, 60 * 60, code.toString());
        return Result.success(MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }

    /**
     * 快速登录时的验证码
     * */

    @RequestMapping("send4Login")
    public Result send4Login(@RequestBody Form form){
        //生成6位数的验证码
        Integer code = ValidateCodeUtils.generateValidateCode(6);
        //发送短信
        try {
           //用户输入的验证码
            String imgCode = form.getImgCode();
            //从redis中取出图片验证码与用户输入的比较
            String deviceId = form.getDeviceId();
            String redisImgCode = jedisUtil.get(deviceId);
            if (!imgCode.equals(redisImgCode)){
                //用户输入的图片验证码不正确
                return Result.error("图片验证码输入错误");
            }
         //   SmsUtil.sendSmsCode(form.getTelephone(),code.toString());
        } catch (Exception e) {
            e.printStackTrace();
            //发送失败
            return Result.error(MessageConstant.SEND_VALIDATECODE_FAIL);
        }
        //发送成功,将验证码存入redis
        jedisUtil.setex(form.getTelephone()+RedisMessageConstant.SENDTYPE_LOGIN,60*60,code.toString());
        return Result.success(MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }
}